package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.DrawerMultipleCompleteEvent;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.fragment.DeskDrawerMultipleFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.contract.bean.RefreshEvent;
import com.wwwarehouse.contract.bean.ReleaseSupplyGoodsListBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;


/**
 * 发布供应列表页
 * Created by ShineF on 2017/10/27 0027.
 */

@Route(path = "/contract/ReleaseSupplyListPagerFragment")
public class ReleaseSupplyListPagerFragment extends CommonBasePagerFragment {

    private String mSort;
    private String mSearchText = "";
    private int mSortPosition = 2;
    private ArrayList<String> mSortContentList;
    private String mEffectiveStatus;
    private String mType;
    private List<CardDeskMessageStableCardBean.TasksBeanBuidNames> mTasksBeanBuidNames;
    private String mCardUnitUkid;

    @Override
    public void requestDatas() {
        super.requestDatas();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        ArrayList<FilterBean> mFilterList = new ArrayList<>();
        mSort = "update_time desc";
        mSortContentList = new ArrayList<>();
        Bundle cardBundle = getArguments();
        if (null == cardBundle) {
            return;
        }
        mType = cardBundle.getString("type");
        //info:信息卡片, function功能卡片
        if ("info".equals(mType)) {
            if (peekFragment() instanceof ReleaseSupplyListPagerFragment)
                mActivity.setTitle(R.string.select_release_goods_info);
            CardDeskMessageStableCardBean.TasksBean bean = cardBundle.getParcelable("data");
            if (bean == null) {
                mStateLayout.showSystemView("数据为null", true);
                return;
            }
            mCardUnitUkid = bean.getTaskTypeUkid();
            mTasksBeanBuidNames = bean.getTasksBeanBuidNames();
            if (mTasksBeanBuidNames == null) {
                mStateLayout.showSystemView("数据为null", true);
                return;
            }
            for (CardDeskMessageStableCardBean.TasksBeanBuidNames taskBean : mTasksBeanBuidNames) {
                mFilterList.add(new FilterBean(false, taskBean.getName()));
                mSortContentList.add(taskBean.getId());
            }
            DeskDrawerMultipleFragment deskDrawerMultipleFragment = DeskDrawerMultipleFragment.newInstance("所属业务单位", mFilterList, 1);
            ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerMultipleFragment);
            ((BaseCardDeskActivity) mActivity).showFilterBt();
            mEffectiveStatus = null;
            requestHttp(null, null, null);
        } else if ("function_edit".equals(mType)) {
            if (peekFragment() instanceof ReleaseSupplyListPagerFragment)
                mActivity.setTitle(R.string.eidt_release_goods);
            CardDeskFunctionResponseBean.TaskBean bean = (CardDeskFunctionResponseBean.TaskBean)
                    cardBundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
            if (null == bean) {
                mStateLayout.showSystemView("数据为null", true);
                return;
            }
            String businessId = bean.getBusinessId();
            mSortContentList.add(businessId);
            ((BaseCardDeskActivity) mActivity).hideFilterBt();
            mEffectiveStatus = "20";
            requestHttp(null, null, mEffectiveStatus);
        }
    }

    /**
     * 网络
     *
     * @param sort   筛选
     * @param search 搜索
     * @param status 状态:20生效, 40未生效
     */
    public void requestHttp(String sort, String search, String status) {

        Map<String, Object> map = new HashMap<>();
        map.put("contractStatus", status);
        map.put("demanderBusinessName", search);
        map.put("page", 1);
        map.put("size", 7);
        map.put("sort", sort);
        map.put("supplierBusinessIds", mSortContentList);
        loadData(ContractConstant.RELEASE_GOODS_LIST, map, 1);
    }

    @Override
    public void onRefreshListener() {
        if (mType.equals(ContractConstant.RELEASE_SUPPLY_INFO)) {
            requestHttp(mSort, mSearchText, mEffectiveStatus);
        } else if (mType.equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
            mEffectiveStatus = "20";
            requestHttp(mSort, mSearchText, mEffectiveStatus);
        }
    }

    @Override
    public void setTitle() {
        super.setTitle();
        if (mType == null) {
            return;
        }
        if (!(peekFragment() instanceof ReleaseSupplyListPagerFragment)) {
            return;
        }
        if (mType.equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
            mActivity.setTitle(R.string.eidt_release_goods);
        } else if (mType.equals(ContractConstant.RELEASE_SUPPLY_INFO)) {
            mActivity.setTitle(R.string.select_release_goods_info);
        }

    }

    @Override
    public void onLoadSuccess(String s, int i) {
        if (i == 1) {
            ReleaseSupplyGoodsListBean bean = JSON.parseObject(s, ReleaseSupplyGoodsListBean.class);
            if (bean != null) {
                ArrayList<ReleaseSupplyGoodsListBean.ListBean> list = bean.getList();
                if (null == list) {
                    showEmptyView(R.drawable.search_empty, R.string.invite_search_no_result, false, true);
                    return;
                }
                try {
                    Bundle bundle = new Bundle();
                    bundle.putString("sortName", mSort);
                    bundle.putString("searchText", mSearchText);
                    bundle.putStringArrayList("filt", mSortContentList);
                    bundle.putString("status", mEffectiveStatus);
                    bundle.putString("type", mType);
                    bundle.putString("cardUnitUkid", mCardUnitUkid);
                    setData(bean.getTotal(), 7, ReleaseSupplyListDetailsFragment.class.getName(), bundle, list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onLoadError(String s) {

    }

    @Override
    public void sortClick(View anchor) {
        super.sortClick(anchor);
        if (peekFragment() instanceof ReleaseSupplyListPagerFragment) {
            showSortPopWindow(anchor);
        }
    }

    @Override
    public void filterClick() {
        super.filterClick();
        ((BaseCardDeskActivity) mActivity).showDrawerLayout();
    }

    @Override
    public void cancelClick() {
        super.cancelClick();
        if (peekFragment() instanceof ReleaseSupplyListPagerFragment) {
            mSearchText = null;
            requestHttp(mSort, null, mEffectiveStatus);
        }
    }

    @Override
    public void textNoRealChanged(String text) {
        super.textNoRealChanged(text);
        if (peekFragment() instanceof ReleaseSupplyListPagerFragment) {
            if (TextUtils.isEmpty(text)) {
                return;
            }
            mSearchText = text;
            requestHttp(mSort, mSearchText, mEffectiveStatus);
        }

    }

    /**
     * 筛选
     *
     * @param event event模型
     */
    public void onEventMainThread(DrawerMultipleCompleteEvent event) {
        if (event != null && event.getSelectedPositionSet() != null && peekFragment() instanceof ReleaseSupplyListPagerFragment) {
            mSortContentList.clear();
            Set<Integer> selectedPositionSet = event.getSelectedPositionSet();
            if (selectedPositionSet != null && !selectedPositionSet.isEmpty()) {
                for (Integer integer : selectedPositionSet) {
                    mSortContentList.add(mTasksBeanBuidNames.get(integer).getId());
                    LogUtils.showErrLog(mTasksBeanBuidNames.get(integer).getName());
                }
            } else {
                for (CardDeskMessageStableCardBean.TasksBeanBuidNames mTasksBeanBuidName : mTasksBeanBuidNames) {
                    mSortContentList.add(mTasksBeanBuidName.getId());
                }
            }
            requestHttp(mSort, mSearchText, mEffectiveStatus);
        }
    }

    public void onEventMainThread(Object object) {
        if (object instanceof RefreshEvent) {
            requestHttp(mSort, mSearchText, mEffectiveStatus);
        }
    }

    public void showSortPopWindow(View view) {
        PopUpUtils.showBubbleInviteSort(mActivity, view, mSortPosition, new PopUpUtils.OnChoiceListener() {
            @Override
            public void onChoice(int i) {
                mSortPosition = i;
                if (2 == i) {
                    mSort = "update_time desc";
                } else if (3 == i) {
                    mSort = "create_time desc";
                }
                requestHttp(mSort, mSearchText, mEffectiveStatus);
            }
        }, "", getString(R.string.sort_1), getString(R.string.sort_2));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
